/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.posplus.losspreventiondataimporter.importer;

import com.posplus.lossprevention.APDAOFactory;
import com.posplus.lossprevention.mapping.Departments;
import com.posplus.strack.mapping.Department;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.posplus.losspreventiondataimporter.service.DepartmentService;
import com.posplus.losspreventiondataimporter.service.DepartmentsService;

/**
 *
 * @author scottw
 */
public class DepartmentsImporter {
    private APDAOFactory lpFactory;
    private DepartmentService departmentsService;
   
    public DepartmentsImporter(DepartmentService departmentsService, APDAOFactory lpFactory){
        this.lpFactory = lpFactory;
        this.departmentsService = departmentsService;
    }
    
    public void dataImport(){
        List<Department> departments = departmentsService.getAll();
        mapAndPersist(departments, lpFactory);
    }
    
    private static void mapAndPersist(List<Department> departments, APDAOFactory lpFactory){
        List<Departments> mappedDepartments = new ArrayList<Departments>();
        
        Iterator<Department> iter = departments.iterator();
        while (iter.hasNext()) {
            Department next = iter.next();
            
            Departments department = new Departments(next.getId().getDeptNo().intValue(), next.getId().getDeptSub().intValue(), next.getDeptName(), null, null);
            mappedDepartments.add(department);
        }
        
        if(mappedDepartments.size() > 0){
            DepartmentsService deptService = new DepartmentsService(lpFactory);
            //deptService.bulkInsert(mappedDepartments);
	    deptService.saveAll(mappedDepartments);
        }
    }
}
